package io.github.iogogogo.kafka.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * Created by tao.zeng on 2024/12/25.
 */
@Slf4j
@Component
public class KafkaSender {

    private KafkaTemplate<String, String> kafkaTemplate;

    public void send(String topic, String msg) {
        if (kafkaTemplate == null) {
            log.warn("未启用secondKafka，不发送消息");
            return;
        }

        CompletableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, msg);
    }

    @Autowired(required = false)
    @Qualifier("secondKafkaTemplate")
    public void setKafkaTemplate(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }
}
